<?php

namespace app\admin\model\xykeep;

use think\Db;
use think\Model;
use traits\model\SoftDelete;

class Distribution extends Model
{

    use SoftDelete;

    

    // 表名
    protected $name = 'xykeep_distribution';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'status_text',
        'one_child_num_text',
        'two_child_num_text',
    ];


    protected static function init()
    {
        self::afterUpdate(function ($row) {
            $pk = $row->getPk();
            $original = $row->getQuery()->where($pk, $row[$pk])->find();

            //同步下user表
            if(isset($row['pid']) && $original['pid'] !== $row['pid']){
                Db::name('user')->where('id', $original[$pk])->update(['xykeep_parent_user_id' => $row['pid']]);
                //上级分销商人员 -1
//                $originalParentUser = Db::name('xykeep_distribution')->where('user_id', $original['pid'])->find();
//                if($originalParentUser){
//                    Db::name('xykeep_distribution')->where('user_id', $original['pid'])->setDec('one_child_num',1);
//
//                    //上上级分销商人员 -1
//                    if($originalParentUser['pid']!==0){
//                        $grandFather = Db::name('xykeep_distribution')->where('user_id', $originalParentUser['pid'])->find();
//                        if($grandFather){
//                            Db::name('xykeep_distribution')->where('user_id', $originalParentUser['pid'])->setDec('two_child_num',1);
//                        }
//
//                    }
//                }
            }
        });
    }

    //获得该用户下的一级分销商数量
    public function getOneChildNumTextAttr($value, $data)
    {
        return Distribution::where(['pid'=>$data['user_id']])->count();
    }

    //获得该用户下的一二级分销商数量
    public function getTwoChildNumTextAttr($value, $data)
    {
        $oneChildList = Distribution::where(['pid'=>$data['user_id']])->select();
        $twoChildNum = 0;
        foreach ($oneChildList as $item) {
            $twoChildNum += Distribution::where(['pid'=>$item['user_id']])->count();
        }
        return $twoChildNum;
    }
    
    public function getStatusList()
    {
        return ['normal' => __('Status normal'), 'forbidden' => __('Status forbidden'),'freeze' => __('Status freeze')];
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function user()
    {
        return $this->belongsTo('\app\admin\model\xykeep\User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function level()
    {
        return $this->belongsTo('\app\admin\model\xykeep\distribution\Level', 'level_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function parent()
    {
        return $this->belongsTo('\app\admin\model\xykeep\User', 'pid', 'id', [], 'LEFT')->setEagerlyType(0);
    }


}
